package com.lcf.app.minio.service;

import io.minio.*;
import io.minio.messages.Item;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;

@Service
public class MinioService {

    @Autowired
    private MinioClient minioClient;

    /**
     * 创建桶
     */
    public void makeBucket(String bucketName) throws Exception {
        boolean bucketExists = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
        if (!bucketExists) {
            minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
        }
    }

    /**
     * 删除桶
     */
    public void removeBucket(String bucketName) throws Exception {
        minioClient.removeBucket(RemoveBucketArgs.builder().bucket(bucketName).build());
    }

    /**
     * 上传本地文件
     */
    public ObjectWriteResponse upload(String bucketName, String object, String fileName) throws Exception {
        makeBucket(bucketName);
        return minioClient.uploadObject(UploadObjectArgs.builder()
                .bucket(bucketName)
                .object(object)
                .filename(fileName)
                .build());
    }

    /**
     * 通过流上传文件
     */
    public ObjectWriteResponse upload(String bucketName, String object, InputStream in) throws Exception {
        makeBucket(bucketName);
        return minioClient.putObject(PutObjectArgs.builder()
                .bucket(bucketName)
                .object(object)
                .stream(in, in.available(), -1)
                .build());
    }

    /**
     * 通过 web 上传文件
     */
    public ObjectWriteResponse upload(String bucketName, String object, MultipartFile file) throws Exception {
        makeBucket(bucketName);
        return minioClient.putObject(PutObjectArgs.builder()
                .bucket(bucketName)
                .object(object)
                .stream(file.getInputStream(), file.getSize(), -1)
                .build());
    }

    /**
     * 下载文件到本地
     */
    public void download(String bucketName, String object, String fileName) throws Exception {
        minioClient.downloadObject(DownloadObjectArgs.builder().bucket(bucketName).object(object).filename(fileName).build());
    }

    /**
     * 下载文件 - 输出到流
     */
    public GetObjectResponse download(String bucketName, String object) throws Exception {
        return minioClient.getObject(GetObjectArgs.builder()
                .bucket(bucketName)
                .object(object)
                .build());
    }

    /**
     * 删除文件
     */
    public void removeObject(String bucketName, String object) throws Exception {
        minioClient.removeObject(RemoveObjectArgs.builder().bucket(bucketName).object(object).build());
    }

    /**
     * 查询桶下的所有文件
     *
     * @param bucketName
     * @return
     */
    public Iterable<Result<Item>> list(String bucketName) {
        return minioClient.listObjects(ListObjectsArgs.builder().bucket(bucketName).build());
    }

    /**
     * 自定义查询文件
     *
     * @param listObjectsArgs
     * @return
     */
    public Iterable<Result<Item>> list(ListObjectsArgs listObjectsArgs) {
        return minioClient.listObjects(listObjectsArgs);
    }

}
